<?php
declare (strict_types=1);

namespace app\serviceapp;

use app\model\GoodsMatchParam;
use app\traits\ServiceTrait;
use think\facade\Db;

/**
 * 商品匹配属性参数
 * Class GoodsMatchParamService
 * @package app\service
 */
class GoodsMatchParamService
{
    //模型，带命名空间
    public static $model = 'app\model\GoodsMatchParam';
    //模型主键
    public static $pk = 'id';

    use ServiceTrait;

    /**
     * 根据分类ID和groupIds数据集查询goodsId
     * @param $cidGroupIdArr
     * @return array
     */
    public static function getGoodsIdByCidAndGroupId($cidGroupIdArr)
    {
        $tabel = GoodsMatchParam::$_table;
        $sql = 'SELECT goods_id, ratio FROM ' . $tabel . " WHERE ";
        foreach ($cidGroupIdArr as $c) {
            $sql .= " (sub_category_id = " . $c['sub_category_id'] . " AND match_group_id = " . $c['match_group_id'] . ') OR';
        }
        $sql = rtrim($sql, 'OR');
        $r = Db::query($sql);
        if (empty($r)) {
            return $r;
        }
        $uniqArr = array_unique($r, SORT_REGULAR);
        return $uniqArr;
    }

    /**
     * 根据商品ID获取ratio
     * @param $goodsId
     * @return string
     */
    public static function getRatioByGoodsId($goodsId)
    {
        $tabel = GoodsMatchParam::$_table;
        $ratioData = Db::name($tabel)->where(['goods_id' => $goodsId])->column('ratio');
        $ratio = '0.00';
        if (!empty($ratioData)) {
            $ratio = $ratioData[0];
        }
        return $ratio;
    }
}
